package com.example.reactor;


import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class CombinationExample {
    public static void main(String[] args) {
        // 示例 1: 使用 Mono 组合操作符
        Mono<String> mono1 = Mono.just("Hello");
        Mono<String> mono2 = Mono.just("World");
        Mono<String> combined = mono1.zipWith(mono2, (a, b) -> a + " " + b);
        combined.subscribe(System.out::println); // 输出: Hello World

        Mono<Void> when = Mono.when(mono1, mono2);
        when.subscribe(null, Throwable::printStackTrace, () -> System.out.println("Completed")); // 输出: Completed

        // 示例 2: 使用 Flux 组合操作符
        Flux<String> flux1 = Flux.just("A", "B", "C");
        Flux<String> flux2 = Flux.just("1", "2", "3");
        Flux<String> merged = Flux.merge(flux1, flux2);
        merged.subscribe(System.out::println); // 输出: A 1 B 2 C 3

        Flux<String> concatenated = Flux.concat(flux1, flux2);
        concatenated.subscribe(System.out::println); // 输出: A B C 1 2 3

        Flux<String> zipped = Flux.zip(flux1, flux2, (a, b) -> a + b);
        zipped.subscribe(System.out::println); // 输出: A1 B2 C3

        Flux<String> combinedLatest = Flux.combineLatest(flux1, flux2, (a, b) -> a + b);
        combinedLatest.subscribe(System.out::println); // 输出: C3

        Flux<String> started = flux1.startWith("Start");
        started.subscribe(System.out::println); // 输出: Start A B C
    }
}